<?php
/*
 * File: UserWalletRecord.php
 * Project: NewDemo
 * Created Time: 2024-07-03 02:25:52 pm
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-09-13 5:08:09 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2023 - 2024 成都睿創科技有限公司
 */


namespace App\Models;

use DateTimeInterface;
use App\Models\Base;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;


/**
 * App\Models\UserWalletRecord
 *
 * @property int $id
 * @property int|null $user_id 用户id
 * @property int|null $type 1：购买2：充值3：提现
 * @property string|null $explain 说明
 * @property float|null $money 金额
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @mixin \Eloquent
 */
class UserWalletRecord extends Base{

    
    use Notifiable;
    protected $table = 'user_wallet_record';
    protected $primaryKey = 'wastage_id';
    public $timestamps = true;
    /**
     * 为数组 / JSON 序列化准备日期。
     */
    protected function serializeDate(DateTimeInterface $date){
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }



    /**
     * 函數構造
     */
    public function __construct(){
        $this->language = app('global_language') == "en" ? "en_" : "";
    }
/*********************************************************全局作用域注册**********************************************************************/

    protected static function boot(){
       parent::boot();
	   static::addGlobalScope('user_wallet_record', function (Builder $builder) {
		  return $builder->where('user_wallet_record.wastage_status', '<>', -1);
	   });
    }
/*********************************************************admin**********************************************************************/

/**********************************************************api**********************************************************************/

    /**
     * 獲取用戶錢包明細
     * @param $data
     * @param $user
     * @return mixed
     */
    public function getUserWallet($data){
        $results = User::with(['getUserWallet'=>function($query) use($data){
            if(!empty($data['wallet_type'])){
                $query->where('wallet_type',$data['wallet_type'])->orderBy('user_wallet_record.created_at','desc')->select('*');
            }else{
                $query->orderBy('user_wallet_record.created_at','desc')->select('*');
            }        
        }])->where('user_id',$data['user_id'])
           ->select('user_id','user_name','user_phone','user_avatar','user_balance')
           ->first();
        $results['get_user_wallet_num']=empty(count($results['getUserWallet'])) ? 0 : count($results['getUserWallet']);
        $limit = empty($data['limit']) ? 10 : $data['limit'];
        $page = empty($data['page']) ? 1 : $data['page'];  
        $min=($page - 1) * $limit;
        $getUserWalletArray=array_slice( json_decode($results['getUserWallet'] , true) , $min, $limit);
        unset($results['getUserWallet']);
        $results['getUserWallet']=$getUserWalletArray;
        if(!empty($results)){
             // $this->LogInfo('獲取收貨地址列表' , $effects , 'getUserAddress' , true);//开启日志记录
            return $this->success( __('api.list_success'), $results);
        }else{
            return $this->success( __('api.list_null'), $results);
        }
    }

    /**
     * 添加錢包明細記錄
     * @param $userId
     * @param $money
     * @return bool
     */
    public function insertWallet($userId,$money){
        $info = [
            'user_id' => $userId,
            'type' => 1,
            'explain' => '購買商品',
            'money' => $money,
            'created_at' => date('Y-m-d H:i:s')
        ];
        $data = $this->insert($info);
        if($data){
            return true;
        }else{
            return false;
        }
    }

/**************************************************common(api和admin共用)***********************************************************/
}
